﻿using System;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using Omu.Encrypto;
using Uber.Core.Model;
using Uber.Data;
using Uber.Service;
using Uber.WebUI.Controllers;

namespace Uber.WebUI
{
    public static class Worker
    {
        public static void Start()
        {
            const int period = 10 * 60000;
            new Timer(Work, null, period, period);
        }

        public static void Work(object a)
        {
            var m = new MailSender(new SmtpServerConfig());
            try
            {
                using (var r = new Db())
                {
                    var d = DateTime.Now.AddSeconds(-10);
                    var ns = r.Set<Notification>().Where(o => o.SendEmail && !o.IsRead && o.Date < d);

                    foreach (var n in ns)
                    {
                        m.SendEmailAsync("noreply@awesome.com", n.Email, NotifyMailTitle(n) + " - awesome forums", NotifyMailBody(n));
                        n.SendEmail = false;
                    }

                    r.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Raize();
            }
        }

        private static string NotifyMailBody(Notification n)
        {
            var h = ConfigurationManager.AppSettings["host"];
            var sb = new StringBuilder();
            sb.Append("You have a new ");
            if (n.NType == 0 )
                sb.AppendFormat("answer on your question: {1},\n click here: {0} to go to the question", h + "/Forum/Questionn/" + n.PostId + "?nid=" + n.Id, n.Post.Title);
            else
                if (n.NType == 1 && n.Post.ParentId.HasValue)
                    sb.AppendFormat("comment on your answer for: {1},\n click here {0} to go to the question", h + "/Forum/Questionn/" + n.Post.ParentId + "?nid=" + n.Id, n.Post.Parent.Title);
                else
                    if (n.NType == 1 && !n.Post.ParentId.HasValue)
                        sb.AppendFormat("comment on your question for: {1},\n click here {0} to go to the question", h + "/Forum/Questionn/" + n.PostId + "?nid=" + n.Id, n.Post.Title);
                    else
                        if (n.NType == 2 && !n.Post.ParentId.HasValue)
                            sb.AppendFormat("comment reply on: {1},\n click here: {0} to go to the question", h + "/Forum/Questionn/" + n.PostId + "?nid=" + n.Id, n.Post.Title);
                        else
                            if (n.NType == 2 && !n.Post.ParentId.HasValue)
                                sb.AppendFormat("comment reply on:{1},\n click here: {0} to go to the question", h + "/Forum/Questionn/" + n.Post.ParentId + "?nid=" + n.Id, n.Post.Parent.Title);
            sb.AppendLine("\n");
            sb.AppendLine("If you no longer want to receive mail from Awesome,");
            var hasher = new Hasher { SaltSize = 13 };

            sb.AppendFormat("Unsubscribe from all Emails with a single click here: {0}", h + "/Account/Unsubscribe/" + n.AccountId + "?q=" + System.Web.HttpUtility.HtmlEncode(hasher.Encrypt(n.Account.CreationDate.ToString())));

            return sb.ToString();
        }

        private static string NotifyMailTitle(Notification n)
        {
            if (n.NType == 0)
                return ("new answer on your question");

            if (n.NType == 1 && n.Post.ParentId.HasValue)
                return ("new comment on your answer");

            if (n.NType == 1 && !n.Post.ParentId.HasValue)
                return ("new comment on your question");

            return ("new comment reply");
        }
    }
}